<script lang="ts" setup="">
import useMainStore from '@/store'
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { getValInObj } from '@/assets/utils/utils'

interface tagItem {
    title: string
    path: string
}

const store = useMainStore(),
    router = useRouter(),
    route = useRoute()
let tagList = computed(() => store.navTagList)

function handle_tagClick(path: string) {
    router.push(path)
}

function handle_tagClose(title: string) {
    const result = getValInObj('title', title, tagList.value),
        index = tagList.value.indexOf(result as never)
    store.handle_closeTag(title)
    const item = tagList.value[index] ?? tagList.value[index - 1]
    if (item) {
        ;(result as tagItem).path === route.fullPath &&
            router.push((item as tagItem).path)
    } else router.push('/home/statistic')
}
</script>

<template>
    <a-breadcrumb class="breadcrumb">
        <a-breadcrumb-item
            class="pointer"
            v-for="data in tagList"
        >
            <a-tag
                @click="handle_tagClick(data.path)"
                @close="handle_tagClose(data.title)"
                closable
                :key="data.path"
                :class="route.path === data.path ? 'active' : ''"
                >{{ data.title }}
            </a-tag>
        </a-breadcrumb-item>
    </a-breadcrumb>
</template>

<style lang="scss" scoped>
.breadcrumb {
    height: 45px;
    line-height: 45px;
    margin: 0 20px 6px 20px;

    & :deep(.ant-breadcrumb-separator) {
        display: none;
    }

    & :deep(.ant-tag) {
        line-height: 32px;
        padding: 0 10px;
        font-size: 14px;

        &:hover {
            color: #38c3ff;
            border-color: #38c3ff;
        }
    }

    &:deep(.ant-tag-close-icon) {
        margin-left: 6px;

        &:hover {
            color: #38c3ff;
        }
    }
}

.active {
    color: #38c3ff;
    border-color: #38c3ff;
    font-size: 14px;
    font-weight: 600;
}
</style>
